import vue from 'vue'
import routers from 'vue-router'
import config from './router.config'
import store from '../store'
import routerList from './routeList'
let list = routerList
vue.use(routers)
let router = new routers({
    mode: 'history',
    // scorllBehavior: () => ({
    //     y: 0

    // }),
    routes:config
})
// function initRoute () {
//   let userInfo = {
//     name: Cookies.get('userName'),
//     id: Cookies.get('userCode')
//   }
//   store.dispatch('user/loginByUserCode', userInfo)
// }
// 处理刷新的时候vuex被清空但是用户已经登录的情况
if (window.sessionStorage.userInfo) {
    store.dispatch('setUserInfo', JSON.parse(window.sessionStorage.userInfo));
}

// 登录中间验证，页面需要登录而没有登录的情况直接跳转登录
router.beforeEach((to, from, next) => {
    console.log('todsasd', to)
    if (to.fullPath) {
      if (to.fullPath.indexOf('_=') !== -1) {
        let key = to.fullPath.substring(to.fullPath.indexOf('_=') + 2)
        if (!window.localStorage.getItem(key)) {
          alert('参数不正确')
        }
      }
    }
    let isPermission = false
    list.forEach((v) => {
    // 判断跳转的页面是否在权限列表中
    if (to.fullPath.indexOf(v) >= 0) {
      isPermission = true
    }
  })
  // 初始化Store
//   initRoute()
  // 没有权限时跳转到登陆页面
  if (!isPermission) {
    next({path: '/login', replace: true})
  } else {
    next()
  }
});
export default router